package cn.gov.pbc.gd.credit.web.controller.corp;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.liu.jbase.ResultVo;
import com.liu.jbase.controller.impl.BaseControllerImpl;

import cn.gov.pbc.gd.credit.common.model.UserModel;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.common.util.UserUtil;
import cn.gov.pbc.gd.credit.user.service.corp.ICorpService;
import cn.gov.pbc.gd.credit.user.service.realuser.IRealUserService;
import cn.gov.pbc.gd.credit.user.service.user.IUserService;
import cn.gov.pbc.gd.credit.user.service.user.impl.UserServiceImpl;
import cn.gov.pbc.gd.credit.vo.corp.CorpVo;
import cn.gov.pbc.gd.credit.vo.corp.ResCorpVo;
import cn.gov.pbc.gd.credit.vo.realuser.RealUserVo;
import cn.gov.pbc.gd.credit.vo.user.UserVo;

@Controller
@RequestMapping("/corp")	
public class CorpController extends BaseControllerImpl<ResCorpVo, CorpVo, ICorpService> {
	
	@Autowired
	private ICorpService corpService;
	@Autowired
	private IRedisService redisService;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IRealUserService realUserService;
	
	/**
	 * 
	 * @param id 分行ID，
	 * @param request 根据分行ID，查询当前分行以及其下级分行。
	 * @return
	 */
	@ResponseBody
    @RequestMapping("/list/by/id")
	public ResultVo<List<ResCorpVo>> listById(String id, HttpServletRequest request)  throws Exception{
    	ResultVo<List<ResCorpVo>> res = corpService.listById(id, request);
		return res;
	}
	
	@ResponseBody
    @RequestMapping("tree/list")
	public ResultVo<List<ResCorpVo>> treeList(CorpVo corpVo, HttpServletRequest request)  throws Exception{
		return corpService.treeCropList(corpVo, request);
	}
	
	@RequestMapping("tree/page")
	public String treePage(CorpVo corpVo, Model model, HttpServletRequest request)  throws Exception{
		
		model.addAttribute("result", corpService.treeCropList(corpVo, request));
		
		return "corp/page";
	}
	/**
	 * 获取当前登录用户所在的机构的corpId
	 * @param corpVo
	 * @return
	 */
	@ResponseBody
    @RequestMapping("getCorpId")
	public String getCorpId(CorpVo corpVo){
		UserModel userModel = UserUtil.getUserModel(corpVo.getSid(), redisService);
		return userModel.getCorpId();
	}
	/**
	 * 根据分行ID，查询当前分行以及其下级分行。（使用/list/by/id前台接收不到数据）
	 * @param id
	 * @param request
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="getCorpListByCorpId",produces = "application/json;charset=utf-8")
	public String getCorpListByCorpId(String id, HttpServletRequest request){
		JSONObject resultJson=new JSONObject();
		ResultVo<List<ResCorpVo>> resultVoList = corpService.listById(id, request);
		resultJson.put("result", resultVoList.getData());
		return resultJson.toString();
	}
	/*@ResponseBody
    @RequestMapping("/update")
	@Override
	public ResultVo<UserVo> update(UserVo userVo, HttpServletRequest request)
			throws Exception {
		String realUserList=userVo.getRealUserList();
		String[] realUserArray=realUserList.split(";");
		for(int g=0;g<realUserArray.length;g++){
			RealUserVo realUserVo=new RealUserVo();
			realUserService.update(realUserVo, request);
		}
		return userService.update(record, request);
	}*/
}
